﻿(function ($) {
    var dialogCountrySettings = {
        'width': 490,
        'title': 'Countries',
        'CountryCode': null,
        'CountryId': null,
        'resizable': true,
        'position': [($(window).size.width / 2) / 245, 10],
        'AutoCompleteElement': null,
        'ApplicationMessagesPartial': null,
        'InputElement': null
    };

    var methods = {
        init: function (options) {

            return this.each(function () {
                var that = $(this);

                if (options) {
                    $.extend(dialogCountrySettings, options);
                }

                $('#' + dialogCountrySettings.btn).button({
                    icons: {
                        primary: 'ui-icon ui-icon-zoomin'
                    },
                    text: false
                }).bind('click', function (e) {
                    dialogCountrySettings.CountryCode = $("#" + dialogCountrySettings.InputElement).val();
                    dialogCountrySettings.CountryId = $("#" + dialogCountrySettings.InputElement).attr("data-opus-id");
                    methods.open.call(that);
                    e.preventDefault();
                });

                $("#" + dialogCountrySettings.AutoCompleteElement).opusAutoComplete({
                    url: '/Ref/Countries'
                });

            });
        },
        destroy: function () {
            return this.each(function () {
                $(this).unbind('.selectCountry');
            });
        },
        open: function (options) {
            if (options) {
                $.extend(dialogCountrySettings, options);
            }

            //return this.each(function () {
            var d = $(this).dialog({
                autoOpen: true,
                width: dialogCountrySettings.width,
                position: dialogCountrySettings.position,
                resizable: dialogCountrySettings.resizable,
                title: dialogCountrySettings.title,
                modal: true,
                open: function (event, ui) {
                    $(this).load('/Ref/CountrySelect' + '?id=' + dialogCountrySettings.CountryId);
                },
                buttons: {
                    "Select": function () {
                        if ($("#Dialog_Country_Clear").hasClass('CancelButton')) {
                            alert("Please save or cancel the current operation first");
                        }
                        else {
                            if ($("#Dialog_Country_Description").val() != "") {
                                $.get('/Shared/ApplicationMessages', {}, function (view) {
                                    $("#" + dialogCountrySettings.ApplicationMessagesPartial).html(view);
                                });
                                $("#" + dialogCountrySettings.InputElement)
                                    .attr('data-opus-id', $("#Dialog_Country_Id").val())
                                    .attr('data-opus-code', $("#Dialog_Country_Code").val())
                                    .val($("#Dialog_Country_Description").val())
                                $(this).dialog("close");
                            }
                            else {
                                alert("Please select a Country first or close the dialog");
                            }
                        }
                    },
                    "Close": function () {
                        if ($("#Dialog_Country_Clear").hasClass('CancelButton')) {
                            alert("Please save or cancel the current operation first");
                        }
                        else {
                            $.get('/Shared/ApplicationMessages', {}, function (view) {
                                $("#" + dialogCountrySettings.ApplicationMessagesPartial).html(view);
                            });
                            $(this).dialog("close");
                        }
                    }
                }
            });

            d.bind("dialogresize", function (event, ui) {
                $('#resizeLabel').text('Size: ' + ui.size.width + ',' + ui.size.height);
            });

            return this;
            //});
        }
    };

    $.fn.selectCountry = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.selectCountry');
        }
    };
})(jQuery);
